﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SEO.Entity;
using System.Data.SqlClient;
using System.Data;

namespace SEO.Business
{
    /// <summary>
    /// 分类名称操作
    /// </summary>
    public class BisSort
    {
        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public Sort Get(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [Sort] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            Sort Info = null;
            while (dr.Read())
            {
                Info = new Sort();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.Name = dr["Name"].ToString();
                Info.LockNumber = Convert.ToInt32(dr["LockNumber"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 新增分类名称
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(Sort Info)
        {
            string strSql = "insert into [Sort]([Name],[LockNumber]) values(@Name,@LockNumber)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@LockNumber", Info.LockNumber));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新分类名称
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(Sort Info)
        {
            string strSql = "update [Sort] set [Name]=@Name,[LockNumber]=@LockNumber where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@LockNumber", Info.LockNumber));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.StoredProcedure,
                   "DeleteSort",
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 获取分类名称列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList()
        {
            return SqlHelper.ExecuteDataset(SqlHelper.database, CommandType.Text, "select * from [Sort]").Tables[0];
        }

    }
}
